﻿/*************************************************************
 * Software of blackcore
 *
 * CLRVersion:4.0.30319.296
 * MachineName:SPACE
 * Author:Andy Huang
 * Email:itblackhole@gmail.com
 * DateTime：5/17/2013 11:10:24 AM
 * Function：
 *************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SobFS.Common.Database;
using System.Configuration;
using System.Web.Configuration;

namespace SobFS.Common
{
    public sealed class ConfigHelper
    {
        public static bool GetConfigBool(string key)
        {
            bool flag = false;
            string configString = GetConfigString(key);
            if (!string.IsNullOrWhiteSpace(configString))
            {
                try
                {
                    flag = bool.Parse(configString);
                }
                catch (FormatException)
                {
                }
            }
            return flag;
        }

        public static decimal GetConfigDecimal(string key)
        {
            decimal num = 0M;
            string configString = GetConfigString(key);
            if (!string.IsNullOrWhiteSpace(configString))
            {
                try
                {
                    num = decimal.Parse(configString);
                }
                catch (FormatException)
                {
                }
            }
            return num;
        }

        public static int GetConfigInt(string key)
        {
            int num = 0;
            string configString = GetConfigString(key);
            if (!string.IsNullOrWhiteSpace(configString))
            {
                try
                {
                    num = int.Parse(configString);
                }
                catch (FormatException)
                {
                }
            }
            return num;
        }

        public static string GetConfigString(string key)
        {
            string cacheKey = "AppSettings-" + key;
            object cache = DbCacheHelper.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
                    if (0 < configuration.AppSettings.Settings.Count)
                    {
                        cache = configuration.AppSettings.Settings[key].Value;
                        DbCacheHelper.SetCache(cacheKey, cache, DateTime.Now.AddMinutes(180.0), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return ((cache != null) ? cache.ToString() : null);
        }
    }
}